package com.java.dormitorymanager.mvc.handler;

import com.java.dormitorymanager.pojo.Manager;
import com.java.dormitorymanager.service.IManagerService;
import com.java.dormitorymanager.service.impl.ManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * @program: DormitoryManager
 * @description: 管理员具体逻辑代码
 * @author: ShuangChenYue
 * @create: 2021-05-14 17:45
 **/
public class ManagerHandler
{
    IManagerService managerService = new ManagerServiceImpl();
//=====================================================登录方法==========================================================
    public void managerLogin(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击管理员登录按钮进入该managerLogin方法");
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            Manager manager = managerService.login(name, password);
            req.getSession().setAttribute("filterManager",manager);
            if (manager != null )
            {
                if (("super".equals(manager.getStatus())))
                {
                    System.out.println("您是超级管理员！");
                    //获得所有管理员列表
                    List<Manager> managerList = managerService.findAll();
                    req.setAttribute("managerList",managerList);
                    //只获得超级管理员对象,因为这里做了判断语句，这边所拿到的manager对象只会是超管
                    req.setAttribute("superManager",manager);
                    Manager superManager = (Manager) req.getAttribute("superManager");
                    //将超管设置到session里
                    req.getSession().setAttribute("superManager",superManager);
                    //将所有超管设置到session会话里
                    List<Manager> allSuperManagerList = managerService.findAllSuperManager();
                    req.getSession().setAttribute("allSuperManagerList",allSuperManagerList);
                    req.getRequestDispatcher("/ViewSuperManager").forward(req,resp);
                }else if (("common".equals(manager.getStatus())))
                {
                    System.out.println("您是寝室管理员！");
                    //若登录的时宿管，则此处存的必然是宿管
                    req.setAttribute("dormManager",manager);
                    Manager dormManager = (Manager) req.getAttribute("dormManager");
                    req.getSession().setAttribute("dormManager",dormManager);
                    //找到所有宿管，并设置到session里
                    List<Manager> allCommonManagerList = managerService.findAllCommonManager();
                    req.getSession().setAttribute("allCommonManagerList",allCommonManagerList);
                    req.getRequestDispatcher("/ViewDormManager").forward(req,resp);
                }else  if (("NO".equals(manager.getStatus())))
                {
                    System.out.println("您是寝管，但目前账户被停用，无法登录，请联系超管！");
                    req.setAttribute("alert","您是寝管，但目前账户被停用，无法登录，请联系超管！");
                    req.getRequestDispatcher("/ViewExceptionPage").forward(req,resp);
                }
            }else {
                System.out.println("登陆失败！");
                req.getRequestDispatcher("/html/manager/login.html").forward(req,resp);
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=====================================================注册方法==========================================================
    public void managerRegister(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击管理员注册按钮进入该managerRegister方法");
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            Manager manager = new Manager();
            manager.setName(name);
            manager.setPassword(password);
            managerService.register(manager);
            req.getRequestDispatcher("/html/manager/login.html").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//===========================================修改超管个人信息前查询数据操作================================================
    public void toUpdateSMDetail(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击修改按钮后进入该方法做显示前的查询数据操作");
            //页面点击修改后，会带着一个id来此界面，我们在此获取该id
            String managerId = req.getParameter("id");
            //将其强制类型转换
            int id = Integer.parseInt(managerId);
            //调用方法使用该id去数据库中查找并返回相应的对象
            Manager manager = managerService.findById(id);
            //将返回的对象存在本次请求中
            req.setAttribute("managerForUpdate",manager);
            //上述步骤执行完毕，请求转发至显示页面，返回的对象也会跟着请求一起过去，所以可以在那边获取
            req.getRequestDispatcher("/ViewUpdateSMDetail").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=============================================修改超管个人信息后数据操作==================================================
    public void updateSMDetail(HttpServletRequest req,HttpServletResponse resp)
    {
        //这是在修改超管个人信息页面，点击update按钮后执行的方法。
        //此处将获取到修改后的信息，调用JDBC更新后插入数据库。
        //而后重新在数据库中查找所有超管并返回，重新显示在页面上
        try
        {
            System.out.println("通过update按钮进入该方法");
            String managerId = req.getParameter("id");
            int id = Integer.parseInt(managerId);
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String status = req.getParameter("status");

            Manager manager = new Manager();
            manager.setId(id);
            manager.setName(name);
            manager.setPassword(password);
            manager.setStatus(status);

            managerService.updateSuperManager(manager);
            System.out.println("更新成功");
            req.setAttribute("manager",manager);
            req.getRequestDispatcher("/mvc/queryAllSMBeforeViewSMDetailAgain.do").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//==============================================在重新显示超管列表前查询依次===============================================
    public void queryAllSMBeforeViewSMDetailAgain(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("通过update按钮进入此重新查询操作");
            List<Manager> allSuperManagerListAgain = managerService.findAllSuperManager();
            req.getSession().setAttribute("allSuperManagerListAgain",allSuperManagerListAgain);
            req.getRequestDispatcher("/ViewSMDAfterUpdate").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//===================================================管理寝室管理员页面===================================================
    public void manageCommonManager(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException
    {
        System.out.println("点击宿管按钮进入该操作");
        //该方法是为了进入管理宿管界面所预先处理数据的
        //1.去数据库查找所有status字段值为common的，并返回commonManagerlist，将其存在req.setAttribute中，去显示页面迭代输出
        List<Manager> allCommonManagerList = managerService.findAllCommonManager();
        req.setAttribute("allCommonManagerList",allCommonManagerList);
        //2.再去数据库中查找所有status字段值为NO的，并返回allNOManagerList
        List<Manager> allBannedManagerList = managerService.findAllBannedManager();
        req.setAttribute("allBannedManagerList",allBannedManagerList);
        req.getRequestDispatcher("/ViewManageCommonManager").forward(req,resp);
    }
//=================================================超管通过宿管id删除宿管=================================================
    /*当ViewManageCommonManagerServlet页面点击删除小按钮时，会带着一个指定的宿管id来到这个页面，在此获取那个id，调用deleteById
    然后转发到另一个方法，在那个方法中重新设置req请求
    */
    public void deleteCommonManagerById(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("通过删除宿管按钮进入此方法");
            String commonManagerId = req.getParameter("id");
            int id = Integer.parseInt(commonManagerId);
            managerService.deleteCommonManagerById(id);
            req.getRequestDispatcher("/mvc/commonManagerListAfterDelete.do").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

//===================================================删除宿管后重新查找===================================================
    public void commonManagerListAfterDelete(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击id删除后进入进入此方法重新查找所有宿管而后转发到显示页面");
            List<Manager> allCommonManagerList = managerService.findAllCommonManager();
            req.setAttribute("allCommonManagerList",allCommonManagerList);
            List<Manager> allBannedManagerList = managerService.findAllBannedManager();
            req.setAttribute("allBannedManagerList",allBannedManagerList);
            req.getRequestDispatcher("/ViewManageCommonManager").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//========================================================停用宿管=======================================================
    public void bannedCommonManager(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击停用按钮进入该bannedCommonManager方法");
            //在此处根据id查找到相应的对象，将其status值更新成NO，表示被停用
            String managerId = req.getParameter("id");
            int id = Integer.parseInt(managerId);
            //调用根据id修改状态为0的方法
            managerService.updateCommonManagerStatusToZero(id);
            //当某对象状态被修改，此时请求转发到显示页面
            //重查并将对象返回到req里
            List<Manager> allCommonManagerList = managerService.findAllCommonManager();
            req.setAttribute("allCommonManagerList",allCommonManagerList);
            List<Manager> allBannedManagerList = managerService.findAllBannedManager();
            req.setAttribute("allBannedManagerList",allBannedManagerList);
            req.getRequestDispatcher("/ViewManageCommonManager").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//================================================进入修改页面前的数据准备================================================
    public void toUpdateCMDetail(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("进入修改宿管信息页面前所需要准备的数据");
            String managerId = req.getParameter("id");
            int id = Integer.parseInt(managerId);
            System.out.println(id);
            //此处调根据id返回对应的对象
            Manager manager = managerService.findById(id);
            req.setAttribute("managerForUpdate",manager);
            //而后转发至显示页面即可
            req.getRequestDispatcher("/ViewUpdateCMDetail").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=================================================更新宿管信息后处理数据=================================================
    public void updateCMDetail(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击更新按钮来此");
            //点击更新按钮后，请求转发到此处，在此处获取值，存到manager对象中，并调用更新方法，更新后，将新的manager存到req.setAttribute中，转发至显示界面
            String managerId = req.getParameter("id");
            int id = Integer.parseInt(managerId);
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String status = req.getParameter("selectStatus");
            Manager manager = new Manager();
            manager.setId(id);
            manager.setName(name);
            manager.setPassword(password);
            manager.setStatus(status);
            managerService.updateCommonManager(manager);
            List<Manager> allCommonManagerList = managerService.findAllCommonManager();
            req.setAttribute("allCommonManagerList",allCommonManagerList);
            List<Manager> allBannedManagerList = managerService.findAllBannedManager();
            req.setAttribute("allBannedManagerList",allBannedManagerList);
            req.getRequestDispatcher("/ViewManageCommonManager").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//========================================================添加宿管=======================================================
    public void addCommonManager(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击add后执行此方法");
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            Manager manager = new Manager();
            manager.setName(name);
            manager.setPassword(password);
            //此处调用managerService.addCommonManager()方法
            managerService.addCommonManager(manager);
            //添加完成后转发到重新查询的方法中
            req.getRequestDispatcher("/mvc/showCMDetailAgain.do").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=======================================================添加完成后======================================================
    //点击玩添加按钮后，在去显示界面前，应当先转发到重新查询所有宿管的方法里，重查之后再转发至显示界面。
    public void showCMDetailAgain(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击add后进入这里重新查询一遍所有的宿管");
            List<Manager> allCommonManagerList = managerService.findAllCommonManager();
            req.setAttribute("allCommonManagerList",allCommonManagerList);
            List<Manager> allBannedManagerList = managerService.findAllBannedManager();
            req.setAttribute("allBannedManagerList",allBannedManagerList);
            req.getRequestDispatcher("/ViewManageCommonManager").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//======================================================根据姓名查找宿管==================================================
    public void searchCMByName(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击搜索进入此方法");
            String name = req.getParameter("name");
            //调用managerService.findCommonManagerByName()方法查找。
            Manager manager = managerService.findCommonManagerByName(name);
            //找到后将返回的对象存在req.setAttribute()中
            req.setAttribute("manager",manager);
            //然后再请求转发到显示界面
            req.getRequestDispatcher("/ViewFindCMBySearch").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=================================================通过id删除被停用的宿管=================================================
    public void deleteBannedManagerById(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击被停用的宿管删除按钮进入此操作");
            //在此处获得传过来的id
            String managerId = req.getParameter("id");
            int id = Integer.parseInt(managerId);
            //然后调用managerService.deleteBannedManagerById(int id)的方法删除
            managerService.deleteBannedManagerById(id);
            //删除之后重新进入显示页面。当然，宿管和被停用的宿管还是得重查。
            List<Manager> allCommonManagerList = managerService.findAllCommonManager();
            req.setAttribute("allCommonManagerList",allCommonManagerList);
            List<Manager> allBannedManagerList = managerService.findAllBannedManager();
            req.setAttribute("allBannedManagerList",allBannedManagerList);
            req.getRequestDispatcher("/ViewManageCommonManager").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=================================================通过id启用被停用的宿管=================================================
    public void openBannedManagerById(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("通过启用按钮进入此openBannedManagerById方法");
            //点击启用会传一个id至此，获取该id，强转之后调用managerService.openBannedManagerById(int id)方法
            String managerId = req.getParameter("id");
            int id = Integer.parseInt(managerId);
            managerService.openBannedManagerById(id);
            //而后转发至显示页面。在这之前需要重新查找宿管和被禁用的宿管
            List<Manager> allCommonManagerList = managerService.findAllCommonManager();
            req.setAttribute("allCommonManagerList",allCommonManagerList);
            List<Manager> allBannedManagerList = managerService.findAllBannedManager();
            req.setAttribute("allBannedManagerList",allBannedManagerList);
            req.getRequestDispatcher("/ViewManageCommonManager").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//===========================================================宿管修改自己=================================================
    public void toUpdateDMDetail(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("进入修改宿管信息页面前所需要准备的数据");
            String managerId = req.getParameter("id");
            int id = Integer.parseInt(managerId);
            System.out.println(id);
            //此处调根据id返回对应的对象
            Manager manager = managerService.findById(id);
            req.setAttribute("managerForUpdate",manager);
            //而后转发至显示页面即可
            req.getRequestDispatcher("/ViewUpdateDMDetail").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=========================================================修改自己后处理信息=============================================
    public void updateDMDetail(HttpServletRequest req,HttpServletResponse resp)
    {
        try
        {
            System.out.println("你已进入修改后的方法");
            String managerId = req.getParameter("id");
            int id = Integer.parseInt(managerId);
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String status = req.getParameter("status");
            Manager manager = new Manager();
            manager.setId(id);
            manager.setName(name);
            manager.setPassword(password);
            manager.setStatus(status);
            managerService.updateCommonManager(manager);
            //若用户修改成功，则需要跳转至登陆界面重新登陆
            req.getRequestDispatcher("/html/manager/login.html").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

